using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Irc.Objects
{
	public sealed class Servername : Target, IComparable, IFormattable
	{
		private string network;
		private string hostmask;
		private int port;
		private bool ssl;
		private List<string> adminInfo = new List<string>();
		private List<Mode> supportedModes = new List<Mode>();
		private Motd motd;

		public string Hostmask
		{
			internal set { hostmask = value; }
			get { return hostmask; }
		}
		public string Network
		{
			internal set { network = value; }
			get { return network; }
		}
		public int Port
		{
			internal set { port = value; }
			get { return port; }
		}
		public bool Ssl
		{
			internal set { ssl = value; }
			get { return ssl; }
		}
		public Collection<string> AdminInfo
		{
			internal set
			{
				lock(this)
				{
					adminInfo.Clear();
					foreach(string s in value)
						adminInfo.Add(s);
				}
			}
			get { return new Collection<string>(adminInfo); }
		}
		public Collection<Mode> SupportedModes
		{
			internal set
			{
				lock(this)
				{
					supportedModes.Clear();
					supportedModes.AddRange(value);
				}
			}
			get { return new Collection<Mode>(supportedModes); }
		}
		public Motd MessageOfTheDay
		{
			internal set { motd = value; }
			get { return motd; }
		}
		public Servername(string hostmask, int port, bool ssl)
		{
			this.hostmask = hostmask;
			this.port = port;
			this.ssl = ssl;
		}
		public Servername(string hostmask) : this(hostmask, 6667, false) {}
		public Servername(string hostmask, int port) : this(hostmask, port, false) {}

		public override string ToString()
		{
			return ToString("G", System.Globalization.CultureInfo.CurrentCulture);
		}
		public int CompareTo(object obj)
		{
			return Hostmask.CompareTo(obj);
		}
		public override bool Equals(object obj)
		{
			return CompareTo(obj) == 0;
		}
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
		public string ToString(string format, IFormatProvider formatProvider)
		{
			return Hostmask;
		}
		public static bool operator==(Servername obj1, Servername obj2)
		{
			// prevent infinite recursion
			if(((object)obj1) == null || ((object)obj2) == null)
				if(((object)obj1) == null && ((object)obj2) == null)
					return true;
				else
					return false;
			if(((object)obj1) != null)
				return obj1.Equals(obj2);
			else return ((object)obj2) == null;
		}
		public static bool operator!=(Servername obj1, Servername obj2)
		{
			return !(obj1 == obj2);
		}
		public static int operator>(Servername obj1, Servername obj2)
		{
			if(obj1 != null)
				return obj1.CompareTo(obj2);
			else return 1;
		}
		public static int operator<(Servername obj1, Servername obj2)
		{
			if(obj2 != null)
				return obj2.CompareTo(obj1);
			else return 1;
		}
		public static implicit operator string(Servername server)
		{
			if(server != null)
				return server.ToString();
			else return String.Empty;
		}
	}
}
